package com.yunxin.core.infra.common.product.util;

import cn.hutool.core.util.ReflectUtil;
import com.yunxin.core.infra.persistence.entity.product.ProductQualityConfig;

import java.lang.reflect.Field;
import java.util.*;

public class ProductQualityUtil {


    //需要校验的字段
    private static Map<Field,Field> FIELDS_MAP= new HashMap<>();


    static {

        Field[] fields = ReflectUtil.getFields(ProductQualityConfig.class);


        for (Field field : fields) {
            String fieldName = ReflectUtil.getFieldName(field);
            if (fieldName.endsWith("Switch")) {
                String valName = fieldName.substring(0, fieldName.indexOf("Switch"));
                Field val = ReflectUtil.getField(ProductQualityConfig.class, valName);
                FIELDS_MAP.put(field, val);
            }
        }

    }


    /**
     * 校验开关 开启后值是否存在
     * @param config
     * @return
     */
    public static boolean checkFull(ProductQualityConfig config) {

        if (Objects.isNull(config)) {
            return false;
        }

        Set<Map.Entry<Field, Field>> entries = FIELDS_MAP.entrySet();
        for (Map.Entry<Field, Field> fieldEntry : entries) {
            Field key = fieldEntry.getKey();
            Field val = fieldEntry.getValue();
            Object fieldValue = ReflectUtil.getFieldValue(config, key);
            if (fieldValue instanceof Integer) {
                int v = (int) fieldValue;
                if (v!=1) {
                    continue;
                }
                Object value = ReflectUtil.getFieldValue(config, val);
                if (Objects.isNull(value)) {
                    return false;
                }
            }

        }
        return true;
    }

}
